package bigdata.backend.datasource.test;

import bigdata.backend.datasource.entity.Datasource;
import bigdata.backend.datasource.mapper.DatasourceMapper;
import bigdata.backend.datasource.service.DatasourceService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.Map;

@SpringBootTest
public class TestDatasourceService {
    @Autowired
    private DatasourceMapper datasourceMapper;

    @Autowired
    private DatasourceService datasourceService;

    public Datasource getDataSource(String datasourceId) {
        Datasource ds = datasourceMapper.selectById(datasourceId);
        return ds;
    }

    @Test
    public void test() {
        Datasource ds = getDataSource("1001");
        List<String> allTables = datasourceService.getAllTables(ds);
        System.out.println(allTables);
    }

    @Test
    public void test1() {
        Datasource ds = getDataSource("3001");
        List<Map<String, Object>> columns = datasourceService.getTableColumns(ds, "test_user");
        for (Map<String, Object> column : columns) {
            System.out.println(column);
        }
    }

    @Test
    public void test2() {
        Datasource ds = getDataSource("3001");
        String comment = datasourceService.getTableComment(ds, "test_user");
        System.out.println(comment);
    }

    @Test
    public void test3() {
        Datasource ds = getDataSource("1001");
        List<Map<String, Object>> list = datasourceService.executeQuery(ds, "SELECT * FROM test_user WHERE name = ? AND age = ?", "bob", 18);
        for (Map<String, Object> map : list) {
            System.out.println(map);
        }
    }

    @Test
    public void test4() {
        Datasource ds = getDataSource("2001");
        String sql = datasourceService.getCreateTableSql(ds, "test_mysql2doris");
        System.out.println(sql);
    }
}
